package com.definesys.smecrailtransit.module.MonthForm.PopWindowAdapter;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.api.SmecRailApi;
import com.definesys.smecrailtransit.base.SmecBaseRecyclerViewHolder;
import com.definesys.smecrailtransit.databinding.LayoutPopMonthItemBinding;
import com.definesys.smecrailtransit.module.MonthForm.MonthFormFragment;
import com.definesys.smecrailtransit.module.MonthForm.dialog.MonthFormDialog;
import com.definesys.smecrailtransit.module.SiteDetails.dialog.SiteRunDialog;
import com.hwangjr.rxbus.Bus;

import java.util.ArrayList;

/**
 * Created by cjy on 2017/8/27.
 */

public class PopMonthAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder>  {

    private ArrayList<String> list;
    protected Bus mRxbus;
    private String type;

    public PopMonthAdapter(ArrayList<String> list,Bus mRxbus, String type){
        if(list != null && list.size() != 0) {
            this.list = list;
        }else{
            this.list = new ArrayList<>();
        }
        this.mRxbus = mRxbus;
        this.type = type;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutPopMonthItemBinding layoutPopweindowItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                , R.layout.layout_pop_month_item,parent,false);
        SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                SmecBaseRecyclerViewHolder(layoutPopweindowItemBinding.getRoot());
        smecBaseRecyclerViewHolder.setViewType(viewType);
        smecBaseRecyclerViewHolder.setBinding(layoutPopweindowItemBinding);
        return smecBaseRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
        LayoutPopMonthItemBinding viewDataBinding=(LayoutPopMonthItemBinding)holder.getBinding();
        viewDataBinding.text.setText(list.get(position));
        viewDataBinding.text.setOnClickListener(v -> {
            if(type.equals(SmecRailApi.MonthCode.MONTHFORM)){
                this.mRxbus.post(MonthFormDialog.CHANGE_POP_MONTH, list.get(position));
            }
            if(type.equals(SmecRailApi.MonthCode.SITEDETAILS)){
                this.mRxbus.post(SiteRunDialog.CHANGE_POP_MONTH, list.get(position));
            }

        });
    }

    @Override
    public int getItemCount() {
        return this.list.size();
    }
}
